#pragma once
//#include <string>
#include <assert.h>
#include <iostream>

namespace zzzyh {
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;
		vector()
			:_start(nullptr),
			_finish(nullptr),
			_end_of_storage(nullptr)
		{}
		vector(int size,const T& x = T())
		{
			T* tmp = new T[size];
			_start = _finish = tmp;
			_end_of_storage = tmp + size;
			for (size_t i = 0; i < size; i++)
			{
				push_back(x);
			}
		}

		template <class InputIterator>
		vector(InputIterator first, InputIterator last)
		{
			while (first != last) {
				push_back(*first);
				first++;
			}
		}

		size_t size() const 
		{
			return _finish - _start;
		}

		size_t capacity() const
		{
			return _end_of_storage - _start;
		}

		void reserve(size_t n) {
			size_t sz = size();
			size_t oldCapacity = capacity();
			if (n <= oldCapacity) {
				return;
			}
			int newCapacity = n > oldCapacity*2 ? n : oldCapacity * 2;
			T* rem = new T[newCapacity];
			if (sz != 0) {
				//memmove(rem, _start, sizeof(T) * sz);
				for (size_t i = 0; i < sz; i++)
				{
					rem[i] = _start[i];
				}
				//delete[] _start;
			}
			delete[] _start;
			_start = rem;
			_finish = rem + sz;
			_end_of_storage = rem + newCapacity;
		}

		T& operator[](int i) {
			return *(_start + i);
		}

		const T& operator[](int i) const 
		{
			return *(_start + i);
		}

		iterator begin() {
			return _start;
		}
		iterator end() {
			return _finish;
		}

		T pop_back() {
			if (!empty()) {
				T ret = *(_finish - 1);
				_finish--;
				return ret;
			}
		}

		bool empty() {
			return _finish == _start;
		}

		const_iterator begin() const
		{
			return _start;
		}
		const_iterator end() const
		{
			return _finish;
		}

		vector(const vector& tmp)
		{
			size_t size = tmp.size();
			reserve(size);
			for (auto& x : tmp) {
				push_back(x);
			}
		}

		void swap(vector<T>& tmp) {
			std::swap(this->_start, tmp._start);
			std::swap(this->_finish, tmp._finish);
			std::swap(this->_end_of_storage, tmp._end_of_storage);
		}

		void clear() {
			_finish = _start;
		}

		//template<class T>
		vector<T>& operator=(const vector<T>& tmp) {
			if (this == &tmp) {
				return *this;
			}
			vector<T> v(tmp);
			swap(v);
			return *this;
		}


		void print_vector() const
		{
			const_iterator beg = begin();
			while (beg != end())
			{
				std::cout << *beg << " ";
				beg++;
			}
			std::cout << std::endl;
		}

		iterator insert(iterator pos, const T& data) {
			assert(pos >= _start && pos <= _finish);
			if (pos == _finish) {
				push_back(data);
				return pos+1;
			}
			if (_finish == _end_of_storage) {
				int i = pos - _start;
				reserve(capacity() == 0 ? 1 : capacity() * 2);
				pos = _start + i;
			}
			iterator end = this->end()-1;
			while (end >= pos) {
				//memmove(end + 1, end, sizeof(T));
				*(end + 1) = *(end);
				end--;
			}
			new(pos)T(data);
			_finish++;
			return pos+1;
		}

		void push_back(const T& data) {
			if (_finish == _end_of_storage) {
				reserve(capacity() == 0 ? 1 : capacity() * 2);
			}
			new(_finish)T(data);
			//*_finish = data;
			_finish++;
		}
		~vector() {
			delete[] _start;
			_start = _finish = _end_of_storage = nullptr;
		}

		iterator erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			if (pos == _finish - 1) {
				pop_back();
				return pos-1;
			}
			//delete (this[pos - _start]);
			//int i = pos - _start;
			//memmove(pos, pos + 1, sizeof(T) * (size() - 1 - (pos - _start)));
			iterator left = pos;
			while (left!=_finish-1) {
				*left = *(left + 1);
				left++;
			}
			//pos = _start+i;
			_finish--;
			return pos;
		}

		void resize(size_t n,const T& x = T()) {
			if (n == size()) {
			}
			else if (n < size()) {
				_finish = _start + n;
			}
			else {
				reserve(n);
				int i = n-size() ;
				while (i > 0)
				{
					this->push_back(x);
					i--;
				}
			}
			return;
		}

		

	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _end_of_storage = nullptr;

	};

	template<class T>
	void print_vector(const vector<T>& v)
	{
		//tempnam vector<T>::const_iterator beg = v.begin();
		typename vector<T>::const_iterator beg =  v.begin();
		while (beg != v.end())
		{
			std::cout << *beg << " ";
			beg++;
		}
		std::cout << std::endl;
	}
}